package com.zhangyu.shadow.repair.delegate.irregular;

import com.zhangyu.shadow.repair.base.IStringDataConvert;
import com.zhangyu.shadow.repair.config.ErrorMsg;
import com.zhangyu.shadow.repair.config.RequestUrls;
import com.zhangyu.shadow.repair.delegate.bean.VehicleBean;

import org.greenrobot.greendao.annotation.NotNull;

import java.util.ArrayList;

import st.lib.base.net.callback.IError;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/7
 */
public class IrregularBusPresenter extends IrregularBusContract.Presenter {

    private final IrregularBusContract.View mView;
    private final IrregularBusContract.Model mModel;
    /**
     * 是否正在请求
     */
    private boolean isRefreshing = false;

    public IrregularBusPresenter(@NotNull IrregularBusContract.View view) {
        mView = view;
        mModel = new IrregularBusModel();
    }

    private void getHistoryExceptionList(String code, ArrayList<String> busVmTypeName,
                                         ArrayList<Integer> classificationId, String dateStart, String dateEnd, String depId) {
        if (isRefreshing) {
            return;
        }
        if (mView != null) {
            mView.showLoading("");
        }
        addDisposable(mModel.getHistoryList(RequestUrls.URL_HISTORY_DATA_LIST,
                busVmTypeName,
                classificationId,
                depId,
                code,
                dateStart,
                dateEnd,
                0,
                new IStringDataConvert<ArrayList<VehicleBean>>() {
                    @Override
                    public void convert(ArrayList<VehicleBean> list) {
                        if (list != null) {
                            if (mView != null) {
                                mView.hideLoading();
                                mView.getVehicleListSuccess(list);
                            }
                        } else {
                            if (mView != null) {
                                mView.getVehicleListFailed(-1, ErrorMsg.ERROR_DATA);
                            }
                        }
                        isRefreshing = false;
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        if (mView != null) {
                            mView.getVehicleListFailed(code, msg);
                        }
                        isRefreshing = false;
                    }
                })
        );
    }

    /**
     * final String key, String groupId,
     *
     * @param dateStart 开始时间
     * @param dateEnd   结束时间
     * @param depId     分厂ID
     */
    private void getReadyVehicleList(int type,
                                     String code,
                                     String dateStart,
                                     String dateEnd,
                                     String minDate,
                                     int id,
                                     String depId) {
        String url = RequestUrls.URL_COMPLETE_PENDING_BUS_LIST;
        int requestType = 0;
        switch (type) {
            case 0:
                url = RequestUrls.URL_COMPLETE_PENDING_BUS_LIST;
                requestType = 0;
                break;
            case 1:
                url = RequestUrls.URL_EXCEPTION_QUERY_LIST;
                requestType = 2;
                break;
            case 2:
                url = RequestUrls.URL_EXCEPTION_QUERY_LIST;
                requestType = 1;
                break;
        }

        addDisposable(mModel.getVehicleList(url,
                depId,
                code,
                dateStart,
                dateEnd,
                requestType,
                minDate,
                id,
                new IStringDataConvert<ArrayList<VehicleBean>>() {
                    @Override
                    public void convert(ArrayList<VehicleBean> list) {
                        if (mView != null) {
                            mView.getVehicleListSuccess(list);
                        }
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        if (mView != null) {
                            mView.getVehicleListFailed(code, msg);
                        }
                    }
                })
        );
    }

    @Override
    public void getVehicleList(int type, String code, String deptId, String minDate,int id) {
        getReadyVehicleList(type, code, "", "", minDate,id, deptId);
    }
}
